package core.validate;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateRule extends Rule<DateRule> {
    public DateRule(String key) {
        super(key);
        this.actions.add(holder -> {
            Date value = null;
            if(holder.getValue() != null) {
                if(holder.getValue() instanceof String && !holder.<String>getValue().isEmpty()) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        value = format.parse(holder.getValue());
                    } catch (Exception ignored) {}
                }
            }
            holder.setStatus(value != null).setValue(value).setMessage("数据类型错误");
        });
    }

    protected DateRule inst() {
        return this;
    }
}